﻿namespace Huayou.UI.WebControls
{
    using System;
    using System.Collections;
    using System.Reflection;
    using System.Xml;

    public class MenuItems : Hashtable
    {
        private XmlElement _groupNode;
        private WebMenu _menu;
        private MenuGroup _parentGroup;

        public MenuItems(XmlElement GroupNode, WebMenu Menu)
        {
            this._groupNode = GroupNode;
            this._menu = Menu;
            this._parentGroup = new MenuGroup(this._groupNode, this._menu);
        }

        public MenuItem Add()
        {
            XmlElement newChild = this._groupNode.OwnerDocument.CreateElement("MenuItem");
            this._groupNode.AppendChild(newChild);
            return new MenuItem(newChild, this._parentGroup, this._menu);
        }

        public void Add(MenuItem item)
        {
            this._groupNode.AppendChild(item.ItemNode);
            item.Menu = this._menu;
        }

        public MenuItem Add(int index)
        {
            XmlElement newChild = this._groupNode.OwnerDocument.CreateElement("MenuItem");
            this._groupNode.InsertBefore(newChild, this._groupNode.ChildNodes[index]);
            return new MenuItem(newChild, this._parentGroup, this._menu);
        }

        public override void Clear()
        {
            for (int i = this._groupNode.ChildNodes.Count - 1; i >= 0; i--)
            {
                this._groupNode.RemoveChild(this._groupNode.ChildNodes[i]);
            }
        }

        private XmlElement FindMenuItemElement(XmlElement ParentGroup, string ItemID)
        {
            for (int i = 0; i < ParentGroup.ChildNodes.Count; i++)
            {
                if ((ParentGroup.ChildNodes[i].Attributes["ID"] != null) && (ParentGroup.ChildNodes[i].Attributes["ID"].Value == ItemID))
                {
                    return (XmlElement) ParentGroup.ChildNodes[i];
                }
            }
            return null;
        }

        public void Remove(int key)
        {
            this._groupNode.RemoveChild(this._groupNode.ChildNodes[key]);
        }

        public void Remove(string key)
        {
            for (int i = 0; i < this._groupNode.ChildNodes.Count; i++)
            {
                if ((this._groupNode.ChildNodes[i].Attributes["ID"] != null) && (this._groupNode.ChildNodes[i].Attributes["ID"].Value == key))
                {
                    this._groupNode.RemoveChild(this._groupNode.ChildNodes[i]);
                }
            }
        }

        public override int Count
        {
            get
            {
                return this._groupNode.ChildNodes.Count;
            }
        }

        public MenuItem this[string key]
        {
            get
            {
                MenuItem item;
                try
                {
                    item = new MenuItem(this.FindMenuItemElement(this._groupNode, key), this._parentGroup, this._menu);
                }
                catch
                {
                    throw new Exception("Item '" + key + "' not found.");
                }
                return item;
            }
        }

        public MenuItem this[int key]
        {
            get
            {
                if ((this._groupNode.ChildNodes.Count - 1) < key)
                {
                    throw new Exception("Item [" + key.ToString() + "] not found.");
                }
                return new MenuItem((XmlElement) this._groupNode.ChildNodes[key], this._parentGroup, this._menu);
            }
        }
    }
}

